package cn.com.generaldata.os.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.impl.sql.SqlTemplate;
import org.nutz.dao.sql.Criteria;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import cn.com.generaldata.jsme_indicator_system.entity.Indicator;
import cn.com.generaldata.jsme_indicator_system.entity.IndicatorIndustry;
import cn.com.generaldata.os.vo.IndicatorIndustryQueryVO;

@Service
public class IndustryIndicatorService {
	private static Logger logger = LoggerFactory.getLogger(IndustryIndicatorService.class);
	@Autowired
	private Dao dao;
	@Autowired
	@Qualifier("sqlTemplate")
	private SqlTemplate sqlTemplate;
	/**
	 * 获取产业下的指标
	 * @param name
	 * @return
	 */

	public List<IndicatorIndustry> findIndustryIndicators(String industryId) {

		Criteria cri = Cnd.cri();
		cri.where().andEquals("industryId", industryId);
		List<IndicatorIndustry> lst = dao.query(IndicatorIndustry.class,cri);
		return lst;
	}

//	public LigerGridPage queryByPage(IndustryIndicatorVO iio) {
//		Criteria cri = Cnd.cri();
//			cri.where().andEquals("industryId", iio.getIndustryId());
//		return ligerGridQuery(cri, iio.getPageNum() == null ? 1 : iio.getPageNum(),
//				iio.getPageSize() == null ? Constants.DEFAULT_PAGE_SIZE : iio.getPageSize(), "");
//	}

	public List<IndicatorIndustry> queryIndustryIndicators(String industryIds) {
		String[] industryArry = industryIds.split(",");
		List<IndicatorIndustry> industryIndicators = Lists.newArrayList();
		for (String industry : industryArry)
			industryIndicators.addAll(findIndustryIndicators(industry));
		return industryIndicators;
	}
	
	public List<IndicatorIndustry> findIndustryIndicatorByReport(String userid,String typeId) {
		String sql="select tii.* from t_INDICATOR_INDUSTRY tii inner join T_REPORT_TYPE_INDICATOR rti"
				+ " on RTI.INDICATOR_ID=TII.INDICATOR_ID inner join TBL_USER_INDUSTRY tui on tui.industry_id=tii.industry_id"
				+ " where RTI.REPORT_TYPE_ID=@typeId and tui.user_id=@userid";
		Map params = Maps.newHashMap();
		params.put("userid", userid);
		params.put("typeId", typeId);
		List<IndicatorIndustry> list = sqlTemplate.query(sql, params,
				IndicatorIndustry.class);
		return list;
	}
	
	/**
	 * 更新产业与指标关系
	 */
	@Transactional
	public void updateIndicatorRel(String dustryId, String[] indicatorIds) {
		Criteria cri = Cnd.cri();
		cri.where().andEquals("industryId", dustryId);

		//-----------------------------
		//| 删除原有的产业下指标关系
		//-----------------------------
		dao.clear(IndicatorIndustry.class,cri);

		//-----------------------------
		//| 新增产业与指标关系
		//-----------------------------
		List<IndicatorIndustry> rels = Lists.newArrayList();
		for (String indicatorId : indicatorIds) {
			IndicatorIndustry rel = new IndicatorIndustry();
			rel.setIndustryId(dustryId);
			rel.setIndicatorId(Integer.valueOf(indicatorId));
			rels.add(rel);
		}
		dao.insert(rels);
	}
	
	/**
	 * 根据条件查询指标
	 * 
	 * @param query
	 */
	public List<Indicator> queryList(IndicatorIndustryQueryVO query) {
		String sql = "select ti.id,ti.name from t_indicator ti inner join t_report_type_indicator rti on rti.indicator_id=ti.id"
				+ " inner join tbl_report_type trt on trt.id=rti.report_type_id where 1=1";
		Map params = Maps.newHashMap();
		if (StringUtils.isNotBlank(query.getName())) {
			sql += " and ti.name like @name";
			params.put("name", "%" + query.getName() + "%");
		}
		if (StringUtils.isNotBlank(query.getSysId())) {
			sql += " and trt.stat_system = @sysId";
			params.put("sysId", Integer.valueOf(query.getSysId()));
		}
		if (StringUtils.isNotBlank(query.getVsId())) {
			sql += " and trt.version_id = @vsId";
			params.put("vsId", Integer.valueOf(query.getVsId()));
		}
		if (StringUtils.isNotBlank(query.getTypeId())) {
			sql += " and trt.id = @typeId";
			params.put("typeId", query.getTypeId());
		}
		List<Indicator> list = sqlTemplate.query(sql, params,
				Indicator.class);
		return list;
	}
}
